package com.suse.jd_system_server.controller;

import com.suse.jd_system_server.dao.MapDao;
import com.suse.jd_system_server.dao.UserDao;
import com.suse.jd_system_server.entity.Hotel;
import com.suse.jd_system_server.entity.User;
import com.suse.jd_system_server.epmty.Locations;
import com.suse.jd_system_server.utils.BaiduMapUtil;
import com.suse.jd_system_server.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Map组件Controller层
 */
@RestController
@RequestMapping("/map")
@CrossOrigin()
public class MapController {
    @Autowired
    private UserDao userDao;
    @Autowired
    private MapDao mapDao;
    //为前端地址批量解析JSP提供用户所在城市的酒店名列表
    @GetMapping("/getHotels")
    public Result<ArrayList<String>> getHotels(Long userId){
//        System.out.println("成功进入...");
        User user = userDao.selects(userId);

        String userCity = BaiduMapUtil.getUserCity(user);//获取用户城市部分字段
        ArrayList<String> hotels = BaiduMapUtil.getHotels(userCity);

        if(hotels.isEmpty())return Result.buildFail("暂无此城市酒店信息，请告知客服等待网站数据更新！");
        else{
            Result<ArrayList<String>> result = Result.buildSuccess(hotels);
            result.add("userLng",user.getAddrx());
            result.add("userLat",user.getAddry());
            result.add("city",userCity);
            return result;
        }
    }

    //为导航提供必要的数据
    //输入：userId，hotelName
    //输出：用户经纬度、用户地址名、酒店经纬度
    @PostMapping("/guide")
    public Result guide(@RequestBody Map map){

        //获取参数
        String userId = String.valueOf(map.get("userId"));
        String hotelName = (String) map.get("ends");
        if(userId.isEmpty() || hotelName.isEmpty())return Result.buildFail("参数缺失！");
        //业务查询
        User user = userDao.selects(Long.parseLong(userId));
        Hotel hotel = mapDao.getHotelInfo("hotel_name", hotelName);

        //设置并返回结果集
        if(user == null || hotel == null)return Result.buildFail("查无此地！");
        else{
            Result<Object> result = Result.buildSuccess();
            result.add("starts",user.getAddr());
            result.add("userLng",user.getAddrx());
            result.add("userLat",user.getAddry());
            result.add("hotelLng",hotel.getAddx());
            result.add("hotelLat",hotel.getAddy());
            return result;
        }
    }

    //获取指定酒店的信息
    @GetMapping("/getHotel")
    public Result getHotel(String hotel){
        Hotel hotelInfo = mapDao.getHotelInfo("hotel_name", hotel);
        return Result.buildSuccess(hotelInfo);
    }

    //坐标拾取系统，地址解析
    @PostMapping("/getAddr")
    public Result getAddr(@RequestBody Locations location) throws Exception {
        String address = BaiduMapUtil.getAddress(location.getLng(), location.getLat());
        return Result.buildSuccess(address);
    }

}
